package com.ruoyi.file.controller;

import com.aliyun.oss.OSS;
import com.ruoyi.common.core.utils.file.FileUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.UUID;

/**
 * 通用请求处理
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/common")
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class CommonController
{
    private static final Logger log = LoggerFactory.getLogger(CommonController.class);

    private static final String FILE_DELIMETER = ",";

    @Resource
    private OSS ossClient;

    @PostMapping("/uploadImg")
    public AjaxResult uploadImg(MultipartFile file, Integer status) throws IOException {
        try {
            System.out.println(file);
            //获取文件后缀名
            String originalFilename = file.getOriginalFilename();
            int i = originalFilename.lastIndexOf(".");
            String png = originalFilename.substring(i);
            String originalFilenameNotFormat = file.getOriginalFilename().substring(0, i - 1);
            //存储空间的名字
            String bucketName = "chemiba";
            String dir = "";
            if (status == null){
                return AjaxResult.error("status不能为空");
            }
            if (status == 1) {
                dir = "checkimg";
            } else if(status == 2) {
                dir = "checkvideo";
            } else if(status == 3) {
                dir = "billimg";
            } else if(status == 4) {
                dir = "contractimg";
            }
            //生成新的文件名 brand/：在bucketName里面创建的分组
            //UUID.randomUUID().toString().replace("-", "")
            //UUID.randomUUID().toString().substring(0, 4)
            String fileName = dir + "/" + originalFilenameNotFormat + UUID.randomUUID().toString().substring(0, 4) + png;
            // 上传文件到指定的存储空间（bucketName）并将其保存为指定的文件名称（fileName）。
            ossClient.putObject(bucketName, fileName, file.getInputStream());
            //文件访问地址。
            String url = "https://" + bucketName + ".oss-cn-shenzhen.aliyuncs.com" + "/" + fileName;
            //https://chemiba.oss-cn-shenzhen.aliyuncs.com/itsource/8f76.png
            System.out.println("上传成功返回的：" + url);

            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;

//            return new AjaxResult(HttpStatus.SUCCESS, "操作成功", url);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            // 关闭OSSClient。
            ossClient.shutdown();
        }
    }

}
